package jalus.blog;

import jalus.FMConfigureFactory;
import jalus.Action;
import jalus.service.ConfigureService;
import jalus.service.WidgetService;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import freemarker.template.Template;
import freemarker.template.TemplateException;

public class ConsleServlet extends Action {
	@SuppressWarnings("unchecked")
	public void doGet(HttpServletRequest req, HttpServletResponse resp)	throws IOException {
		UserService userService = UserServiceFactory.getUserService();
		if(!userService.isUserLoggedIn() || !userService.isUserAdmin()){
			error();
			return;
		}
		User user = userService.getCurrentUser();
		
		
		Template template = FMConfigureFactory.getFMConfigure().getTemplate("consle.html");
		Map root = new HashMap();
		root.put("userService", userService);
		root.put("user", user);
		root.put("config", ConfigureService.getInstance());
		root.put("focustab", "manager");
		root.put("uri", req.getRequestURI());
		/* sidebar */
		WidgetService.getInstance().getAllWidget(root);
		
        resp.setCharacterEncoding("utf-8"); 
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = resp.getWriter();
        try {
        	template.process(root, out);
		} catch (TemplateException e) {
			e.printStackTrace();
		}
        out.flush();
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		UserService userService = UserServiceFactory.getUserService();
		if(!userService.isUserLoggedIn() || !userService.isUserAdmin()){
			error();
			return;
		}
		
		ConfigureService config = ConfigureService.getInstance();
		config.setPostnumperpage(Integer.valueOf(req.getParameter("postnumperpage")));
		config.setRecentCommentCount(Integer.valueOf(req.getParameter("recentCommentCount")));
		config.setRecentGuestBookCount(Integer.valueOf(req.getParameter("recentGuestBookCount")));
		config.setFeedCount(Integer.valueOf(req.getParameter("feedCount")));
		config.setRecentPostCount(Integer.valueOf(req.getParameter("recentPostCount")));
		
		
		config.setBlogTitle(req.getParameter("blogTitle"));
		config.setHost(req.getParameter("host"));
		config.setMetaWeblogUserName(req.getParameter("metaWeblogUserName"));
		config.setMetaWeblogPassword(req.getParameter("metaWeblogPassword"));
		config.setPicasaUserName(req.getParameter("picasaUserName"));
		config.setPicasaAlbumName(req.getParameter("picasaAlbumName"));
		config.setGoogleReaderPassword(req.getParameter("googleReaderPassword"));
		config.setGoogleReaderUserName(req.getParameter("googleReaderUserName"));
		config.setDoubanUserId(req.getParameter("doubanUserId"));
		
		config.setHeadText(req.getParameter("headtext"));
		config.setBodyText(req.getParameter("bodytext"));
		config.setFooterText(req.getParameter("footertext"));
		config.setPostText(req.getParameter("posttext"));
		
		config.save();
		resp.sendRedirect("/consle");
	}
}


